package net.mattiraty.imagebrowser;

import java.io.File;
import java.io.FileFilter;

import android.util.Log;

/**
 * Simple filter file list class for getting
 * only specifi files from desired directory.
 *
 */
public class FilteredFileList extends Object {

	private String TAG = "FilteredFileList";
	
	private String mDirectory;	
	
	private String[] mExtensionList;
	private String[] mFiles;
	
	/**
	 * @param directory Directory to scan
	 * @param extensionList List of extensions for files
	 * 					    that should be added to list 
	 */
	public FilteredFileList(String directory,
							String[] extensionList) {
		mDirectory = directory;		
		mExtensionList = extensionList;
	}
	
	/**
	 * Loads image paths.
	 */
	public void load() {
		File dir = new File(mDirectory);
		FileFilter filter = new ImageFilter();
		
		File[] files = dir.listFiles(filter);
		
		mFiles = new String[files.length];
		
		for(int i=0; i < files.length; i++) {
			Log.i(TAG, "Image found:" + files[i].toString());
			
			mFiles[i] = files[i].toString();
		}
		
	}
	
	/**
	 * @return String array of file paths
	 */
	public String[] getFiles(){
		return mFiles;
	}
	
	/**
	 * Poor man's type filter for supported images.
	 *
	 */
	private class ImageFilter implements FileFilter {

		public boolean accept(File arg0) {
			
			for(int i=0; i < mExtensionList.length; i++) {
				if(arg0.toString().endsWith(mExtensionList[i])) {
					return true;
				}
			}
			
			return false;
		}
		
	}

}
